trigger LengthTrigger on MZ_Playlist_Entry__c (after insert , after delete) {
    if (Trigger.isInsert) { 
        map<id ,id  > lengthMap = new map<id ,id>();
        for(MZ_Playlist_Entry__c entry:trigger.new){
            lengthMap.put(entry.MZ_Playlist_ref__c , entry.MZ_Track_ref__c );
        }
        map<id,MZ_Playlist__c> mapPlaylist = new map<id, MZ_Playlist__c>([Select id,MZ_Total_Length__c from MZ_Playlist__c where id=:lengthMap.keySet()]);
    
        map<id,MZ_Track__c > mapTrack = new map <id,MZ_Track__c > ([Select id,MZ_Length__c from MZ_Track__c where id=:lengthMap.values() ]);
        for (id palylistid:lengthMap.keySet() ) {
            mapPlaylist.get(palylistid).MZ_Total_Length__c+=mapTrack.get(lengthMap.get(palylistid)).MZ_Length__c;
          
        }
        
        update mapPlaylist.values();
    }
        if (Trigger.isDelete) { 
        map<id ,id  > lengthMap = new map<id ,id>();
        for(MZ_Playlist_Entry__c entry:trigger.old){
            lengthMap.put(entry.MZ_Playlist_ref__c , entry.MZ_Track_ref__c );
        }
        map<id,MZ_Playlist__c> mapPlaylist = new map<id, MZ_Playlist__c>([Select id,MZ_Total_Length__c from MZ_Playlist__c where id=:lengthMap.keySet()]);
    
        map<id,MZ_Track__c > mapTrack = new map <id,MZ_Track__c > ([Select id,MZ_Length__c from MZ_Track__c where id=:lengthMap.values() ]);
        for (id palylistid:lengthMap.keySet() ) {
            mapPlaylist.get(palylistid).MZ_Total_Length__c-=mapTrack.get(lengthMap.get(palylistid)).MZ_Length__c;
          
        }
        
        update mapPlaylist.values();
    }
}